package com.tj.jinzao.widgets;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.tj.jinzao.R;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.utils.Utils;

import java.util.List;

/**
 * 选择dialog
 * Created by wjy on 2018/6/7.
 */
public class ChooseBottomDialog extends Dialog implements View.OnClickListener {

    protected Activity mContext;
    private List<String> mListStr1, mListStr2, mListStr3;
    private String mDefaultValue1, mDefaultValue2, mDefaultValue3;
    private int mSelectedIndex1, mSelectedIndex2, mSelectedIndex3;
    WheelView mWheelView1;
    WheelView mWheelView2;
    WheelView mWheelView3;

    private int mCount;

    public ChooseBottomDialog(Activity context) {
        super(context, R.style.Theme_Dialog_From_Bottom);
        this.mContext = context;
        this.setCanceledOnTouchOutside(true);
        this.setCancelable(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_choose_wheelview);
        mWheelView1 = findViewById(R.id.wheel_view_1);
        mWheelView2 = findViewById(R.id.wheel_view_2);
        mWheelView3 = findViewById(R.id.wheel_view_3);
        initLayoutParams();
        findViewById(R.id.tv_cancel).setOnClickListener(this);
        findViewById(R.id.tv_confirm).setOnClickListener(this);
        mWheelView1.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                mSelectedIndex1 = selectedIndex - 1;
                if (mListener != null) mListener.onValueChanging(mSelectedIndex1, 0);
            }
        });
        mWheelView2.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                mSelectedIndex2 = selectedIndex - 1;
                if (mListener != null) mListener.onValueChanging(mSelectedIndex2, 1);
            }
        });
        mWheelView3.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                mSelectedIndex3 = selectedIndex - 1;
                if (mListener != null) mListener.onValueChanging(mSelectedIndex3, 2);
            }
        });
        int width = Constant.SCREEN_WIDTH / mCount;
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, ViewGroup.LayoutParams.MATCH_PARENT);
        if (mListStr1 != null) mWheelView1.setLayoutParams(params);
        if (mListStr2 != null) mWheelView2.setLayoutParams(params);
        if (mListStr3 != null) mWheelView3.setLayoutParams(params);
        setWheelViewValue(mWheelView1, mListStr1, mDefaultValue1);
        setWheelViewValue(mWheelView2, mListStr2, mDefaultValue2);
        setWheelViewValue(mWheelView3, mListStr3, mDefaultValue3);

    }

    private void initLayoutParams() {
        // 不能写在init()中
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
        lp.width = dm.widthPixels;
        lp.height = Utils.dpToPx(285, mContext.getResources());
        lp.gravity = Gravity.BOTTOM;
        window.setAttributes(lp);
    }

    public void setData(List<String> data1, List<String> data2, List<String> data3
            , String defaultValue1, String defaultValue2, String defaultValue3) {
        if (data1 != null) mCount++;
        if (data2 != null) mCount++;
        if (data3 != null) mCount++;
        mListStr1 = data1;
        mListStr2 = data2;
        mListStr3 = data3;
        mDefaultValue1 = defaultValue1;
        mDefaultValue2 = defaultValue2;
        mDefaultValue3 = defaultValue3;
        mSelectedIndex1 = defaultValue1 == null ? 0 : Math.max(0, data1.indexOf(defaultValue1));
        mSelectedIndex2 = defaultValue2 == null ? 0 : Math.max(0, data2.indexOf(defaultValue2));
        mSelectedIndex3 = defaultValue3 == null ? 0 : Math.max(0, data3.indexOf(defaultValue3));
    }

    private void setWheelViewValue(WheelView wheelView, List<String> data, String defaultValue) {
        if (data != null) {
            wheelView.setItems(data);
            wheelView.setSelection(Math.max(0, data.indexOf(defaultValue)));
        } else {
            wheelView.setVisibility(View.GONE);
        }
    }

    public void refreshData(int wheelIndex, List<String> data, String defaultValue) {
        if (wheelIndex == 0) {
            mListStr1 = data;
            mDefaultValue1 = defaultValue;
            setWheelViewValue(mWheelView1, data, defaultValue);
        } else if (wheelIndex == 1) {
            mListStr2 = data;
            mDefaultValue2 = defaultValue;
            setWheelViewValue(mWheelView2, data, defaultValue);
        } else if (wheelIndex == 2) {
            mListStr3 = data;
            mDefaultValue3 = defaultValue;
            setWheelViewValue(mWheelView3, data, defaultValue);
        }
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_cancel) {
            dismiss();
        } else if (v.getId() == R.id.tv_confirm) {
            if (mListener != null) {
                String value1 = mListStr1 == null ? null : mListStr1.get(mSelectedIndex1);
                String value2 = mListStr2 == null ? null : mListStr2.get(mSelectedIndex2);
                String value3 = mListStr3 == null ? null : mListStr3.get(mSelectedIndex3);
                mListener.onValueChanged(value1, value2, value3);

            }
            dismiss();
        }
    }

    private OnWheelValueListener mListener;

    public void setValueListener(OnWheelValueListener listener) {
        this.mListener = listener;
    }

    public interface OnWheelValueListener {
        void onValueChanged(String select1, String select2, String select3);

        void onValueChanging(int selectIndex, int wheelIndex);
    }
}
